package com.turing.post.account.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.turing.post.account.util.SubAccountDetail;
import com.turing.post.account.util.SubAccountDetailDao;
import com.turing.post.cardmanagement.util.ActivateCardDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "account.impl.SubAccountDetailDaoImpl")
@Remote
@Local
public class SubAccountDetailDaoImpl implements SubAccountDetailDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;
	@EJB(mappedName = "cardmanagement.impl.ActivateCardDaoImpl")
	private ActivateCardDao acDao;
	/**
	 * 插入子帐户记帐明细记录
	 * 
	 * @param sad
	 * @throws PCPSApplicationException
	 */
	public void insert(SubAccountDetail sad) throws PCPSApplicationException {
		GeneralDao.insert(_em, sad);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SubAccountDetail> getSubAccDetailListByCondition(
			PCPSWebInput webInput, String cardNo, BigInteger subAccNo,
			String startTime, String endTime, Integer page) {
		if(!acDao.checkOperIsInIssueCompany(webInput.getOperBranchNo(), cardNo)){
			return null;
		}
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery("select t.* "
					+ "from subaccountdetail t,activatecard a,masteraccount m,subaccount s "
					+ "where a.masteraccno=m.masteraccno "
					+ "and m.masteraccno=s.masteraccno "
					+ "and s.subaccno=t.subaccno "
					+ "and t.subaccno=?1 " + "and (a.cardno=?2 or a.cardseqno=?2) "
					+ "and t.trantime>=to_date(?3,'yyyy-mm-dd hh24:mi:ss') " 
					+ "and t.trantime<=to_date(?4,'yyyy-mm-dd hh24:mi:ss') "
					+ "order by t.subaccno,t.trantime desc",
					SubAccountDetail.class).setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, subAccNo);
		query.setParameter(2, cardNo);
		query.setParameter(3, startTime);
		query.setParameter(4, endTime);
		List<SubAccountDetail> list = query.getResultList();
		return list;
	}

	@Override
	public BigInteger getSubAccDetailListTotal(PCPSWebInput webInput,
			String cardNo, BigInteger subAccNo, String startTime, String endTime) {
		if(!acDao.checkOperIsInIssueCompany(webInput.getOperBranchNo(), cardNo)){
			return null;
		}
		Query query = _em.createNativeQuery("select nvl(count(*),0) "
					+ "from subaccountdetail t,activatecard a,masteraccount m,subaccount s "
					+ "where a.masteraccno=m.masteraccno "
					+ "and m.masteraccno=s.masteraccno "
					+ "and s.subaccno=t.subaccno "
					+ "and t.subaccno=?1 " + "and (a.cardno=?2 or a.cardseqno=?2) "
					+ "and t.trantime>=to_date(?3,'yyyy-mm-dd hh24:mi:ss') " 
					+ "and t.trantime<=to_date(?4,'yyyy-mm-dd hh24:mi:ss') ");
		query.setParameter(1, subAccNo);
		query.setParameter(2, cardNo);
		query.setParameter(3, startTime);
		query.setParameter(4, endTime);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public SubAccountDetail getSubAccountDetailByAccseqno(
			PCPSWebInput webInput, String subAccno, String accSeqNo) {
		Query query = _em.createNativeQuery(
					"select * from subaccountdetail t where t.subaccno=?1 and t.accseqno='?2'",
					SubAccountDetail.class);
		query.setParameter(1, subAccno);
		query.setParameter(2, accSeqNo);
		return (SubAccountDetail)query.getSingleResult();
	}
}
